package com.ruoyi.paipan.common;

import com.ruoyi.paipan.utils.LunarUtil;
import com.ruoyi.paipan.utils.TaoUtil;

import java.util.List;

public class Tao {
    public static int BIRTH_YEAR = -2697;
    private Lunar lunar;

    public Tao(Lunar lunar) {
        this.lunar = lunar;
    }

    public static Tao fromLunar(Lunar lunar)
    {
        return new Tao(lunar);
    }

    public static Tao fromYmdHms(int year, int month, int day, int hour, int minute, int second)
    {
        return Tao.fromLunar(Lunar.fromYmdHms(year + Tao.BIRTH_YEAR, month, day, hour, minute, second));
    }

    public static Tao fromYmd(int year, int month, int day)
    {
        return Tao.fromYmdHms(year, month, day, 0, 0, 0);
    }

    public Lunar getLunar()
    {
        return this.lunar;
    }

    public int getYear()
    {
        return this.lunar.getYear() - Tao.BIRTH_YEAR;
    }

    public int getMonth()
    {
        return this.lunar.getMonth();
    }

    public int getDay()
    {
        return this.lunar.getDay();
    }

    public String getYearInChinese()
    {
        String y = this.getYear() + "";
        StringBuilder s = new StringBuilder();
        for (int i = 0, j =y.length(); i < j; i++) {
            s.append(LunarUtil.NUMBER.get(Character.codePointAt(y.substring(i, i + 1), 0) - 48));
        }
        return s.toString();
    }

    public String getMonthInChinese()
    {
        return this.lunar.getMonthInChinese();
    }

    public String getDayInChinese()
    {
        return this.lunar.getDayInChinese();
    }

    public List<TaoFestival> getFestivals()
    {
        List<TaoFestival> l = TaoUtil.getFestivals(this.getMonth() + "-" + this.getDay());
        String jq = this.lunar.getJieQi();
        if ("冬至".compareTo(jq) == 0) {
            l.add(new TaoFestival("元始天尊圣诞"));
        } else if ("夏至".compareTo(jq) == 0) {
            l.add(new TaoFestival("灵宝天尊圣诞"));
        }
        if (!TaoUtil.BA_JIE.get(jq).isEmpty()) {
            l.add(new TaoFestival(TaoUtil.BA_JIE.get(jq)));
        }
        String gz = this.lunar.getDayInGanZhi();
        if (!TaoUtil.BA_HUI.get(gz).isEmpty()) {
            l.add(new TaoFestival(TaoUtil.BA_HUI.get(gz)));
        }
        return l;
    }

    private boolean isDayIn(List<String> days)
    {
        String md = this.getMonth() + "-" + this.getDay();
        for (String d : days) {
            if(md.compareTo(d) == 0){
                return true;
            }
        }
        return false;
    }

    public boolean isDaySanHui()
    {
        return this.isDayIn(TaoUtil.SAN_HUI);
    }

    public boolean isDaySanYuan()
    {
        return this.isDayIn(TaoUtil.SAN_YUAN);
    }

    public boolean isDayWuLa()
    {
        return this.isDayIn(TaoUtil.WU_LA);
    }

    public boolean isDayBaJie()
    {
        return !TaoUtil.BA_JIE.get(this.lunar.getJieQi()).isEmpty();
    }

    public boolean isDayBaHui()
    {
        return !TaoUtil.BA_HUI.get(this.lunar.getDayInGanZhi()).isEmpty();
    }

    public boolean isDayMingWu()
    {
        return "戊".compareTo(this.lunar.getDayGan()) == 0;
    }

    public boolean isDayAnWu()
    {
        return this.lunar.getDayZhi().compareTo( TaoUtil.AN_WU.get(Math.abs(this.getMonth()) - 1)) == 0;
    }

    public boolean isDayWu()
    {
        return this.isDayMingWu() || this.isDayAnWu();
    }

    public boolean isDayTianShe()
    {
        boolean ret = false;
        String mz = this.lunar.getMonthZhi();
        String dgz = this.lunar.getDayInGanZhi();
        if ("寅卯辰".contains(mz) ) {
            if ("戊寅".equals(dgz)) {
                ret = true;
            }
        } else if ("巳午未".contains(mz)) {
            if ("甲午".equals(dgz)) {
                ret = true;
            }
        } else if ("申酉戌".contains(mz)) {
            if ("戊申".equals(dgz)) {
                ret = true;
            }
        } else if ("亥子丑".contains(mz)) {
            if ("甲子".equals(dgz)) {
                ret = true;
            }
        }
        return ret;
    }
    @Override
    public String toString()
    {
        return String.format("%s年%s月%s", this.getYearInChinese(), this.getMonthInChinese(), this.getDayInChinese());
    }

    public String toFullString()
    {
        return String.format("道歷%s年，天運%s年，%s月，%s日。%s月%s日，%s時。", this.getYearInChinese(), this.lunar.getYearInGanZhi(), this.lunar.getMonthInGanZhi(), this.lunar.getDayInGanZhi(), this.getMonthInChinese(), this.getDayInChinese(), this.lunar.getTimeZhi());
    }

    public static void main(String[] args) {
        System.out.println(Tao.fromYmd(2021, 1, 1).toFullString());
    }
}
